package com.bcu.model.util;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.bcu.model.sdk.WXPayConstants;
import com.bcu.model.sdk.WXPayUtil;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class PayUtil {
    //获取appid
    public static String getAppId(){

        return "wx9f830c5394927146";
    }

    //获取out_trade_no(商户订单号)
    public static String getOutTradeNo(){
        /**
         * 用于生成订单号
         */
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddhhMMss");
        return sdf.format(new Date());
    }

    //获取spbill_create_ip(终端ip)
    public static String getSpbillCreateIp(){
        /**
         * 服务器 IP 写死
         */
        return "39.105.208.84";
    }

    //获取notify_url(通知地址)
    public static String getNotifyUrl(){
        /**
         * 未知
         */
        return "http://2306.tech/CCShop/dopay";
    }

    //获取Key(API密钥)
    public static String getKey(){

        return "beijingchengshixueyuanxinxixuebu";
    }

    public static String conPayParam(String prepayid){
        Map<String,String> map = new HashMap<String,String>();
        map.put("appId", getAppId()); //小程序ID
        map.put("nonceStr", WXPayUtil.generateNonceStr() );  //随机串
        map.put("package", "prepay_id=" + prepayid);  //数据包
        map.put("signType", "MD5");  //签名方式
        map.put("timeStamp",  WXPayUtil.getCurrentTimestamp()+"");  //时间戳

        try {
            String sign = WXPayUtil.generateSignature(map, getKey(), WXPayConstants.SignType.MD5);
            map.put("paySign", sign);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
        }
        JSONObject jsonObject= JSONUtil.parseObj(map);
        System.out.println("RETURN_JSON"+jsonObject.toString());
        return jsonObject.toString();
    }
}
